﻿using System.Globalization;
using Balloonerds.Algebra;

namespace RayTracer
{
    public class Plane : Thing
    {
        public Vector Normal { get; set; }
        public Vector Origin { get; set; }

        public override Intersection GetIntersection(Ray ray)
        {
            Intersection intersection = null;

            var uv = Normal * ray.Direction;
            if (uv != 0)
            {
                var distance = (Origin - ray.Origin) * Normal / uv;
                var point = ray.ProjectPoint(distance);

                intersection = new Intersection(this, ray, ray.ProjectPoint(distance), distance);
            }

            return intersection;
        }
        public override Vector GetSurfaceNormal(Vector point)
        {
            return Normal;
        }

        public override Thing Translate(Vector position)
        {
            Origin += position;

            return base.Translate(position);
        }
        public override Thing RotateX(double radians)
        {
            Normal = Normal.RotateX(radians);
            Origin = Origin.RotateX(radians);

            return base.RotateX(radians);
        }
        public override Thing RotateY(double radians)
        {
            Normal = Normal.RotateY(radians);
            Origin = Origin.RotateY(radians);

            return base.RotateY(radians);
        }
        public override Thing RotateZ(double radians)
        {
            Normal = Normal.RotateZ(radians);
            Origin = Origin.RotateZ(radians);

            return base.RotateZ(radians);
        }

        public override string ToString()
        {
            return string.Format(NumberFormatInfo.InvariantInfo, "Plane:N{0};O{1}", Normal, Origin);
        }
    }
}
